﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace Charts
{
	public class ValuesDataTemplateSelector : DataTemplateSelector
	{
		public PropertyPath Path { get; set; }
		public string StringFormat { get; set; }
		
		public override DataTemplate SelectTemplate(object item, DependencyObject container)
		{
			DataTemplate template = new DataTemplate();
			FrameworkElementFactory gridElement = new FrameworkElementFactory(typeof(Grid));
			FrameworkElementFactory textBoxElement = new FrameworkElementFactory(typeof(TextBox));
			textBoxElement.SetBinding(TextBox.TextProperty, new Binding() { Path = this.Path, StringFormat = this.StringFormat, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
			gridElement.AppendChild(textBoxElement);
			
			template.VisualTree = gridElement;
			return template;
		}
	}
}
